package com.epam.mentoring.music.behaviour;

import org.jbehave.core.Embeddable;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.i18n.LocalizedKeywords;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.LoadFromRelativeFile;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.junit.JUnitStory;
import org.jbehave.core.model.ExamplesTableFactory;
import org.jbehave.core.reporters.CrossReference;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.reporters.FilePrintStreamFactory.ResolveToPackagedName;

import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
import static org.jbehave.core.reporters.Format.XML;
import static org.jbehave.core.reporters.Format.HTML;
import static org.jbehave.core.reporters.Format.CONSOLE;
import static org.jbehave.core.reporters.Format.TXT;
import org.jbehave.core.steps.ParameterConverters;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

//@RunWith(JUnitReportingRunner.class)
public class MusicUiScenarios extends JUnitStories {

    private final CrossReference xref = new CrossReference();

    public MusicUiScenarios() {
        super();
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration(), new MusicUiSteps());
    }

    @Override
    protected List<String> storyPaths() {
        String codeLocation = codeLocationFromClass(this.getClass()).getFile();
        System.out.println("Code location " + codeLocation);
        //List<String> storyPaths = new StoryFinder().findPaths(codeLocationFromClass(this.getClass()), "**/*.story", "**/*exclude.story");
        List<String> included = Arrays.asList("**/*.story");
        List<String> excluded = Arrays.asList("**/*exclude.story");
        List<String> storyPaths = new StoryFinder().findPaths(System.getProperty("user.dir") + "/src/test/java/com/epam/mentoring/music/behaviour/", included,  excluded, "/com/epam/mentoring/music/behaviour/");
        System.out.println("Stories " + storyPaths);
        return storyPaths;
    }

    @Override
    public Configuration configuration() {
        Class<? extends Embeddable> embeddableClass = this.getClass();
        URL storyURL = null;
        Properties viewResources = new Properties();
        viewResources.put("decorateNonHtml", "true");
        ParameterConverters parameterConverters = new ParameterConverters();
        ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(new LocalizedKeywords(),
                new LoadFromClasspath(embeddableClass), parameterConverters);
        parameterConverters.addConverters(new ParameterConverters.DateConverter(new SimpleDateFormat("yyyy-MM-dd")),
                new ParameterConverters.ExamplesTableConverter(examplesTableFactory));
        try {
            // This requires you to start Maven from the project directory
            storyURL = new URL("file://" + System.getProperty("user.dir") + "/src/test/java");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return new MostUsefulConfiguration()
                .useStoryLoader(new LoadFromRelativeFile(storyURL))
                .useStoryReporterBuilder(new StoryReporterBuilder()
                        .withCodeLocation(codeLocationFromClass(embeddableClass))
                        .withDefaultFormats().withPathResolver(new ResolveToPackagedName())
                        .withViewResources(viewResources).withFormats(CONSOLE, TXT, HTML, XML)
                        .withFailureTrace(true).withFailureTraceCompression(true).withCrossReference(xref));
    }

    //@JavaParametrized.JBehaveStoryPath

  /*
    @Override
    protected List<String> storyPaths() {
        return Arrays.asList("org/musicParser/behaviorTests/music_ui_scenarios.story");
    }
    */
}